package com.forester.foresterplatform.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.forester.foresterplatform.common.utils.JwtUtil;
import com.forester.foresterplatform.system.annotation.OperationLogging;
import com.forester.foresterplatform.system.annotation.SystemLogging;
import com.forester.foresterplatform.system.entity.Result;
import com.forester.foresterplatform.system.entity.User;
import com.forester.foresterplatform.system.enums.OperationType;
import com.forester.foresterplatform.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@Api(tags = "用户管理")
@RequestMapping("/api/v1/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    // 鉴权
    @PreAuthorize("hasAuthority('system:user:list')")
    // swagger
    @ApiOperation(value = "根据用户ID获取用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    // 系统日志
    @SystemLogging
    Result<User> getById(@PathVariable Long id) {
        log.info("查询用户信息，id：{}", id);
        return Result.ok(userService.getById(id));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存用户")
    @PreAuthorize("hasAuthority('system:user:add')")
    Result<Void> save(@RequestBody User user) {
        userService.save(user);
        return Result.ok();
    }

    @PutMapping("/update")
    // swagger
    @ApiOperation(value = "更新用户")
    // 鉴权
    @PreAuthorize("hasAuthority('system:user:edit')")
    Result<Void> update(@RequestBody User user) {
        userService.updateById(user);
        return Result.ok();
    }

    @DeleteMapping("/{id}")
    // 鉴权
    @PreAuthorize("hasAuthority('system:user:remove')")
    // swagger
    @ApiOperation(value = "根据ID删除用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    // 业务操作日志
    @OperationLogging(description = "删除用户", type = OperationType.DELETE)
    Result<Void> deleteById(@PathVariable Long id) {
        boolean b = userService.removeById(id);
        return b ? Result.ok() : Result.error("删除失败");
    }

    @PostMapping("/login")
    // swager
    @ApiOperation(value = "登录")
    Result<String> login(@RequestBody User user) {
        userService.login(user);

        // 生成 Token
        String token = JwtUtil.generateToken(user.getId(), user.getUsername());
        return Result.ok(token);
    }

    @GetMapping("/list")
    // 鉴权
    @PreAuthorize("hasAuthority('system:user:list')")
    // swagger
    @ApiOperation(value = "获取用户列表")
    // 系统日志
    @SystemLogging
    Result<Page<User>> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size,
                            String sort, User user) {
        return Result.ok(userService.page(page, size, sort, user));
    }
}
